$(function () {

    var returnGoods_datagrid = $("#returnGoods_datagrid");
    var returnGoods_dialog = $("#returnGoods_dialog");
    var returnGoods_dialog_datagrid = $("#returnGoods_dialog_datagrid");
    var SelectReturn_dialog = $("#SelectReturn_dialog");
    var SelectReturn_datagrid = $("#SelectReturn_datagrid");
    var returnGoods_dialog_form = $("#returnGoods_dialog_form")


    returnGoods_datagrid.datagrid({
        url:"/returnGoods/list",
        fit:true,
        fitColumns:true,
        rownumbers:true,
        singleSelect:true,
        border:false,
        columns:[[
            {field:"sn",title:"订单编码",width:100},
            {field:"supplier",title:"供应商",width:100,formatter:formatterSupplier},
            {field:"costPrice",title:"单价",width:100},
            {field:"number",title:"数量",width:100},
            {field:"amountPrice",title:"合计",width:100},
            {field:"receipts",title:"实收",width:100},
            {field:"operator",title:"操作人员",width:100},
        ]],
        toolbar:"#returnGoods_toolbar"
    })

    //选中入库单列表
    SelectReturn_datagrid.datagrid({
        url:"/stockincome/getStockList",
        fit: true,
        fitColumns: true,
        singleSelect: true,
        columns: [[
            {field:"sn",title:"订单编码",width:100},
            {field:"supplier",title:"供应商",width:100,formatter:formatterSupplier},
            {field:"costPrice",title:"单价",width:100},
            {field:"number",title:"数量",width:100},
            {field:"amountPrice",title:"合计",width:100},
            {field:"receipts",title:"实收",width:100},
            {field:"operator",title:"操作人员",width:100},
        ]],
        onDblClickRow:function (index,row) {
            console.log(row);
            //1.获取行数据
            returnGoods_dialog_datagrid.datagrid("appendRow",{
                // id:row.id,
                supplier:row.supplier,
                costPrice:row.costPrice,
                productId:row.id,
                number:row.number,
                amountPrice:row.amountPrice,
                receipts:row.receipts,
            });

            SelectReturn_dialog.dialog("close")

        },

    })

    returnGoods_dialog_datagrid.datagrid({
        fit:true,
        fitColumns:true,
        rownumbers:true,
        singleSelect:true,
        border:false,
        columns:[[
            {field:"sn",title:"订单编码",width:100},
            {field:"supplier",title:"供应商",width:100,formatter:formatterSupplier},
            {field:"costPrice",title:"单价",width:100},
            {field:"number",title:"数量",width:100},
            {field:"amountPrice",title:"合计",width:100},
            {field:"receipts",title:"实收",width:100},
            {field:"operator",title:"操作人员",width:100},
        ]],
    })


    cmdObj = {
        //新增退货
        returnGoodsAdd:function () {
            returnGoods_dialog.dialog("open")
        },
        addProduct:function () {
            
        },
        addProduct:function () {
            SelectReturn_dialog.dialog("open")
        },
        return:function () {
            returnGoods_dialog_form.form("submit",{
                url:"/returnGoods/save",
                onSubmit:function (param) {
                    var data = SelectReturn_datagrid.datagrid("getRows");
                    console.log(data);
                    //遍历所有数据，拼接上传参数
                    for(var i = 0;i<data.length;i++){
                        console.log(data);
                        param['id'] = data[i].id;
                        param['sn'] = data[i].sn;
                        param['supplier.id'] = data[i].supplier.id;
                        param['costPrice'] = data[i].costPrice;
                        param['number'] = data[i].number;
                        param['amountPrice'] = data[i].amountPrice;
                        param['receipts'] = data[i].receipts;

                    }
                },
                success:function (data) {
                    //data = $.parseJSON(data);
                    console.log(data)
                    if(data.success){
                        $.messager.alert("温馨提醒","操作成功","info",function () {
                            returnGoods_datagrid.datagrid("reload");
                            SelectReturn_dialog.dialog("close");
                        })
                    }else{
                        $.messager.alert("温馨提醒",data.message,"error");
                    }
                }
            })
        }

    }

    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        cmdObj[cmd]();
    })

})

//供应商
function formatterSupplier(value,row,index) {
    return value ? value.name :"";
}

